package com.topideal.supplychain.hades.util;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.topideal.supplychain.framework.exception.BusinessException;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRGraphics2DExporter;
import net.sf.jasperreports.engine.export.JRGraphics2DExporterParameter;

public class IreportUtil {

    /**
     * @author wanwei
     * @TODO
     * @date: 2017年12月14日 上午10:34:53
     * @param fileName 文件名
     * @param jrxmlSourcePath .jrxml 文件的完整路径
     * @param jasperFilePath 生成 .jasper文件的完整路径
     * @param pdfFilePath 生成 .pdf文件的完整路径
     * @param DataSource 数据源
     * @throws JRException
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static byte[] createPdf(String fileName, InputStream jrxmlSourcePath,
            JRDataSource DataSource) {
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        // 1.将jrxml编译生成a.jasper
        // 2.将a.jasper-->生成pdf文件
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlSourcePath);
            JasperPrint jasperPrint =
                    JasperFillManager.fillReport(jasperReport, parameterMap, DataSource);
            JRGraphics2DExporter exporter = new JRGraphics2DExporter();// 创建graphics输出器
            // 创建一个影像对象
            BufferedImage bufferedImage = new BufferedImage(jasperPrint.getPageWidth() * 2,
                    jasperPrint.getPageHeight() *2, 1);
            // 取graphics
            Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
            // 设置相应参数信息
            exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, g);
            exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, Float.valueOf(2));
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
            exporter.exportReport();
            g.dispose();// 释放资源信息
            // 这里的bufferedImage就是最终的影像图像信息,可以通过这个对象导入到cm中了.
            ImageIO.write(bufferedImage, "png", os);
            byte[] bytes = os.toByteArray();
            return bytes;
        } catch (Exception e) {
            throw new BusinessException("面单生成方法失败:" + e.getMessage() + e);
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                throw new BusinessException("面单生成方法失败：" + e.getMessage() + e);
            }
        }

    }
}
